1151C - Problem for Nazar - CodeForces Solution


constructive algorithms math *1800

Please click on ads to support us..

C++ Code:

/*
Problem: 1151C
Date: 17-01-2024 05:48 AM
*/


#define P 1000000007

#include <iostream>

using namespace std;

long long l, r;

long long sumA(long long l, long long n) {
	return (((l + n - 1) * (l + n - 1) - (l - 1) * (l - 1)) % P + P) % P;
}
long long sumB(long long l, long long n) {
	return (((l + n - 1) * (l + n) - (l - 1) * l) % P + P) % P;
}

long long sumTo(long long n) {
	if(n == 0) {
		return 0;
	}
	long long p = 1;
	long long a = 1;
	long long b = 1;
	long long idx = 0;
	long long sum = 0;
	while(true) {
		if(idx + p >= n) {
			sum = (sum + sumA(a % P, (n - idx) % P)) % P;
			break;
		}else {
			sum = (sum + sumA(a % P, p % P)) % P;
		}
		idx += p;
		a += p;
		p *= 2;

		if(idx + p >= n) {
			sum = (sum + sumB(b % P, (n - idx) % P)) % P;
			break;
		}else {
			sum = (sum + sumB(b % P, p % P)) % P;
		}
		idx += p;
		b += p;
		p *= 2;
	}
	return sum;
}

int main() {
	cin >> l >> r;
	cout << ((((sumTo(r) - sumTo(l - 1)) % P) + P) % P) << endl;
}


Comments

Submit
0 Comments
More Questions

60. Permutation Sequence
42. Trapping Rain Water
32. Longest Valid Parentheses
Cutting a material
Bubble Sort
Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes
Missing numbers
Maximum sum
13 Reasons Why
Friend's Relationship
Health of a person